package me.evis.gae.test;

import java.io.File;

import com.google.appengine.tools.development.ApiProxyLocalImpl;
import com.google.apphosting.api.ApiProxy;

import junit.framework.TestCase;

/**
 * Unit tests against GAE services based classes should extend this test case, 
 * in order to get AppID registered.
 */
public class LocalServiceTestCase extends TestCase {
    
    /**
     * Overrides the JUnit runBare(). Handles Local Service initialization and 
     * clean-up.
     */
    public final void runBare() throws Throwable {
        initLocalService();
        try {
            super.runBare();
        } catch (Exception e) {
            throw e;
        } finally {
            cleanLocalService();
        }
    }
    
    void initLocalService() {
        ApiProxy.setEnvironmentForCurrentThread(new TestEnvironment()); 
        ApiProxy.setDelegate(new ApiProxyLocalImpl(new File(".")){});
    }

    void cleanLocalService() {
        ApiProxy.setDelegate(null);
        ApiProxy.setEnvironmentForCurrentThread(null);
    }
}
